package com.hdoit.ares.logical.cust.dao.impl;

import java.util.List;

import org.springframework.stereotype.Component;

import com.hdoit.ares.logical.cust.dao.ICustFootprintDao;
import com.hdoit.ares.logical.cust.model.CustFootprint;
import com.hdoit.ares.logical.official.model.Artical;
import com.hdoit.ares.logical.ser.model.GoodStore;
import com.hdoit.ares.logical.ser.model.GoodsService;
import com.hdoit.ares.logical.share.model.ShareInfo;
import com.hdoit.ares.logical.share.model.TrainCourse;
import com.hdoit.ares.logical.viewmodel.cust.MyFootprint;
import com.hdoit.ares.selfcommon.enums.cust.CollectAndFootprintType;
import com.tennetcn.common.dao.base.IFinderExpression;
import com.tennetcn.common.dao.base.impl.SuperDao;
import com.tennetcn.common.enums.OrderEnum;
import com.tennetcn.common.utils.FinderExpressionFactory;
/**
 * @author      xieqixiu
 * @email       
 * @createtime  2017年11月27日 23:34:53
 * @comment
 */
@Component
public class CustFootprintDaoImpl extends SuperDao<CustFootprint> implements ICustFootprintDao{

	@Override
	public int deleteFootprint(String type, String custId) {
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		
		finderExpression.delete()
						.from(CustFootprint.class)
						.andEq("type", type)
						.andEq("custId", custId);
		
		return delete(finderExpression);
	}

	@Override
	public List<MyFootprint> selectFootprints(String type, String custId) {
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		
		if(CollectAndFootprintType.infoType.equals(type)){
			finderExpression.select("selectTable.id","selectTable.infoTitle as name","selectTable.infoThumbnailId as pictureId","custFootprint.browseDate as browseDate")
							.from(CustFootprint.class, "custFootprint")
							.leftJoin(ShareInfo.class,"selectTable")
							.on("custFootprint.thingId=selectTable.id");
		}else if(CollectAndFootprintType.videoType.equals(type)){
			finderExpression.select("selectTable.id","selectTable.courseTitle as name","selectTable.courceThumbnailId as pictureId","custFootprint.browseDate as browseDate")
							.from(CustFootprint.class, "custFootprint")
							.leftJoin(TrainCourse.class,"selectTable")
							.on("custFootprint.thingId=selectTable.id");
		}else if(CollectAndFootprintType.storeType.equals(type)){
			finderExpression.select("selectTable.id","selectTable.storeName as name","custFootprint.browseDate as browseDate")
							.from(CustFootprint.class, "custFootprint")
							.leftJoin(GoodStore.class,"selectTable")
							.on("custFootprint.thingId=selectTable.id");
		}else if(CollectAndFootprintType.goodType.equals(type)){
			finderExpression.select("selectTable.id","selectTable.serviceGoodsName as name","selectTable.thumbnailId as pictureId","custFootprint.browseDate as browseDate")
							.from(CustFootprint.class, "custFootprint")
							.leftJoin(GoodsService.class,"selectTable")
							.on("custFootprint.thingId=selectTable.id");	
		}else if(CollectAndFootprintType.insuType.equals(type)){
			
		}
		
		finderExpression.andEq("custFootprint.type", type)
						.andEq("custFootprint.custId", custId)
						.addOrder("browseDate", OrderEnum.desc);
		
		return queryList(finderExpression,MyFootprint.class);
	}

	/*根据类型和顾客Id查询足迹中的相应内容*/
	@Override
	public List<Artical> selectArticalByType(String type, String custId,Integer pageNum,Integer length) {
		String strCustId = custId.trim();
		IFinderExpression oneChildIdFinder=FinderExpressionFactory.createExpression();
		oneChildIdFinder.select("thingId")
						.from(CustFootprint.class)
						.andWhere("type=#{type}")
						.andWhere("custId=#{custId}")
						.addOrder("browseDate", OrderEnum.desc);
		
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select(" * ")
						.from(Artical.class,"artical")
						.andWhere("artical.id in("+oneChildIdFinder.toSql()+")")
						.setParam("type", type)
						.setParam("custId", strCustId);
		
		if(pageNum != null && length !=null){
			int start=(pageNum-1)*length;
			finderExpression.limit(start, length);
		}
		
		return queryList(finderExpression,Artical.class);
	}

	@Override
	public CustFootprint selectCustFootprint(String thingId, String custId) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select(" * ")
						.from(CustFootprint.class,"foot")
						.andWhere("thingId=#{thingId}")
						.andWhere("custId=#{custId}")
						.setParam("thingId", thingId)
						.setParam("custId", custId);
		
		return queryModel(finderExpression);
	}

	/*查询当前顾客足迹中的所有文章和视频*/
	@Override
	public List<Artical> selectAllArtical(String custId,Integer pageNum,Integer length) {
		int start=(pageNum-1)*length;
		IFinderExpression oneChildIdFinder=FinderExpressionFactory.createExpression();
		oneChildIdFinder.select("thingId")
						.from(CustFootprint.class)
						.andWhere("custId=#{custId}")
						.addOrder("browseDate", OrderEnum.desc);
		
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select(" * ")
						.from(Artical.class,"artical")
						.andWhere("artical.id in("+oneChildIdFinder.toSql()+")")
						.setParam("custId", custId)
						.limit(start, length);
		
		return queryList(finderExpression,Artical.class);
	}
	
}